package com.pod5.skype.master;

import java.awt.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
 
public class ManageIP {


private String parseAddressIP(String s)
{
	String result = null;
	try{
	 result = Integer.parseInt(s.substring(0, 2),16)
						+"."+Integer.parseInt(s.substring(2, 4),16)
						+"."+Integer.parseInt(s.substring(4, 6),16)
						+"."+Integer.parseInt(s.substring(6, 8),16);
	}
	catch(NumberFormatException e)
	{
		e.printStackTrace();
	}
	return result;
}

	//Built for one machine parsing and storing information.
 private void addToReadableIP(LinkedList<String> rawIPData, LinkedList<String> readableIP) {

	 if(!rawIPData.isEmpty())
	 {
		 for(int i=0; i<rawIPData.size(); i++){
				String addressIP = parseAddressIP(rawIPData.get(i));
				if(!readableIP.contains(addressIP) && addressIP != null)
					readableIP.add(addressIP);
		 }
	 }
	
 }

private void addToRawIP(LinkedList<String> newRawIPData, LinkedList<String> oldRawIPData){
	
	if(!newRawIPData.isEmpty()){
		for(int i=0; i<newRawIPData.size(); i++){
			if(!oldRawIPData.contains(newRawIPData.get(i)))
				oldRawIPData.add(newRawIPData.get(i));
		}
	}
}

public String getNextIP(LinkedList<String> currentRawIPData, LinkedList<String> newRawIPData,
		LinkedList<String> crawledIPData, LinkedList<String> readableIP){
	
	String nextRawIP = "quit";
	
	addToReadableIP(newRawIPData, readableIP);
	addToRawIP(newRawIPData, currentRawIPData);
	
	if(!currentRawIPData.isEmpty()){
		while(crawledIPData.contains(parseAddressIP(currentRawIPData.getFirst())))
		 {
			currentRawIPData.removeFirst();
			if(currentRawIPData.isEmpty())
				break;
		 }
		if(!currentRawIPData.isEmpty())
		{
		 crawledIPData.add(parseAddressIP(currentRawIPData.getFirst()));
		 nextRawIP = currentRawIPData.getFirst();
		}
	}
	
	
	return nextRawIP;
}



}
